package net.tmaize.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class UrlUtils {

	/**
	 * 由于接口的某些链接无法访问 http://pic1.zhimg.com/81b2747867fdbb7cf82cebf1b83b10c4.jpg
	 * 修改为下面的就可以了 http://pic4.zhimg.com/81b2747867fdbb7cf82cebf1b83b10c4.jpg
	 */
	public static String imageUrlChange_1(String url) {

		String str = null;
		switch (url.charAt(10)) {
		case '1':
			str = url.substring(0, 10) + "4" + url.substring(11, url.length());
			break;
		case '2':
			str = url.substring(0, 10) + "4" + url.substring(11, url.length());
			break;
		case '3':
			str = url.substring(0, 10) + "4" + url.substring(11, url.length());
			break;
		case '4':
			str = url.substring(0, 10) + "3" + url.substring(11, url.length());
			break;
		default:
			throw new RuntimeException("错误的url");
		}
		return str;
	}

	/**
	 * 由于http://daily.zhihu.com/api/2/news/before/20160808获得的是2016.8.7 星期日的数据
	 * 做如下变化 http://daily.zhihu.com/api/2/news/before/20160808
	 * http://daily.zhihu.com/api/2/news/before/20160809
	 * 
	 * @throws ParseException
	 */
	public static String beforeUrlChange(String url) {

		try {
			String date = url.substring(url.length() - 8, url.length());
			String oriUrl = url.substring(0, url.length() - 8);

			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			Date formatDate = dateFormat.parse(date);
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(formatDate);
			//日期向后推移一天
			calendar.add(Calendar.DATE,1);
			formatDate = calendar.getTime();
			return oriUrl+dateFormat.format(formatDate);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
}
